import { ComponentInternalInstance, getCurrentInstance } from "@vue/runtime-core";

export function useUniqueFunc<
  P extends any[],
  T extends (ctx: ComponentInternalInstance, ...args: P) => any
>(key: string, func: T): (...args: P) => ReturnType<T> {
  return (...args) => {
    const ctx = getCurrentInstance() as any;
    if (!ctx._use) ctx._use = {};
    if (ctx._use[key]) {
      throw new Error(`use ${key} can only be executed once!`);
    }

    ctx._use[key] = func;

    return func(ctx, ...args);
  };
}
